package cn.tedu.csmall.lilishop.controller;

import cn.tedu.csmall.lilishop.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.lilishop.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.csmall.lilishop.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.lilishop.pojo.vo.AdminStandardVO;
import cn.tedu.csmall.lilishop.security.LoginPrincipal;
import cn.tedu.csmall.lilishop.service.IAdminService;
import cn.tedu.csmall.lilishop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "01.管理员管理模块")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    public AdminController() {
        log.info("创建控制器：AdminController");
    }

    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("")
    public JsonResult<List<AdminListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员列表】的请求……");
        log.debug("从SecurityContext中获取到的信息："); // 新增
        log.debug("当事人id = {}", loginPrincipal.getId()); // 新增
        log.debug("当事人用户名 = {}", loginPrincipal.getUsername()); // 新增
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 300)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【登录认证】的请求，参数：{}", adminLoginInfoDTO);
        String jwt = adminService.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }
    @ApiOperation("根据id删除管理员")
    @ApiOperationSupport(order =400)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(Long id){
        log.debug("开始处理【根据id删除管理员】的请求：id={}", id);
        adminService.deleteById(id);
        return JsonResult.ok();
    }
}
